package com.autonavi.yunda.yunji.groovy.test

import com.autonavi.yunda.yunji.core.utils.DynamicSqlUtil
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test

/**
 * @author cizhong.wcs
 * @date 2022/10/20 19:35
 */
class DynamicSqlTest {
    @Test
    void testInsert() {
        String sql = DynamicSqlUtil.INSERT("student",
                ["name", "age", "address"],
                [
                        [name: "judy", age: 10, address: "beijing"],
                        [name: "lucy", address: "shanghai"]
                ])
        Assertions.assertThat(sql).isEqualTo("INSERT INTO student (name,age,address) VALUES ('judy','10','beijing'),('lucy',NULL,'shanghai')")
        sql = DynamicSqlUtil.INSERT("student",
                ["name", "age", "address", ["product_id", "productId"]],
                [
                        [name: "judy", age: 10, address: "beijing", productId: 1000],
                        [name: "lucy", address: "shanghai"]
                ])
        Assertions.assertThat(sql).isEqualTo("INSERT INTO student (name,age,address,product_id) VALUES ('judy','10','beijing','1000'),('lucy',NULL,'shanghai',NULL)")
        sql = DynamicSqlUtil.INSERT("student",
                ["name", ["age", null, 666], "address", ["product_id", "productId", 999]],
                [
                        [name: "judy", age: 10, address: "beijing", productId: 1000],
                        [name: "lucy", address: "shanghai"]
                ])
        Assertions.assertThat(sql).isEqualTo("INSERT INTO student (name,age,address,product_id) VALUES ('judy','10','beijing','1000'),('lucy','666','shanghai','999')")


        sql = DynamicSqlUtil.INSERT('student', ['name': 'judy', 'age': 10, 'address': 'beijing'])
        Assertions.assertThat(sql).isEqualTo('INSERT INTO student(name,age,address) values (\'judy\',\'10\',\'beijing\')')

        sql = DynamicSqlUtil.INSERT('student', ['name': 'judy', 'age': 10, 'address': "beijing' palace"])
        println sql
        Assertions.assertThat(sql).isEqualTo("INSERT INTO student(name,age,address) values ('judy','10','beijing'' palace')")

        Assertions.assertThat(DynamicSqlUtil.IN(['a', 'b'])).isEqualTo(" IN ('a','b') ")

        Assertions.assertThat(DynamicSqlUtil.IN("name", "")).isEqualTo(" 1 != 1 ")
        Assertions.assertThat(DynamicSqlUtil.IN("name", [], true)).isEqualTo(" 1 = 1 ")
        Assertions.assertThat(DynamicSqlUtil.IN("name", "a,b")).isEqualTo(" name IN ('a','b') ")
        Assertions.assertThat(DynamicSqlUtil.IN("name", ['a', 'b'])).isEqualTo(" name IN ('a','b') ")
        Assertions.assertThat(DynamicSqlUtil.IN("name", "a,b'".split(','))).isEqualTo(" name IN ('a','b''') ")
        Assertions.assertThat(DynamicSqlUtil.IN("name", "".split(','))).isEqualTo(" name IN ('') ")
    }
}
